import { PeerToPeer } from "../../dist/esm/index.js";
export const init = async () => {
  // 创建点对点连接，连接子页面
  const peer = new PeerToPeer(["#son4", "#son5"], {
    handlers: {
      parentLog: console.log,
    },
  });
  // 建立链接并监听子页面全部加载完成
  await peer.connect().load();
  // 子页面加载完后发送广播消息给所有子页面
  peer.broadcast({ type: "load:finish" });
  const send2Son = document.querySelector("#send2Son");
  const invokeSon = document.querySelector("#invokeSon");
  // 监听子页面消息
  peer.server.on("msg", console.log.bind(null, "parent收到消息"));
  send2Son.addEventListener("click", () => {
    peer.broadcast({ type: "msg", data: { name: "parent" } });
  });
  invokeSon.addEventListener("click", () => {
    peer.invoke({ type: "son4info", data: ["parent"] });
    peer.invoke({ type: "son5info", data: ["parent"] });
  });
};
